package com.example.myapplication.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.service.SocketService;
import com.example.myapplication.tools.MultiThreadServer;
import com.example.myapplication.tools.MySocketServer;
import com.socker_server.MainClass;
import com.socker_server.ServerConfig;
import com.socker_server.entity.IMessageProtocol;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteOrder;

public class SocketServiceActivity extends Activity {

    String TAG = "=========socket ";

    private final int PORT = 8805;

    TextView tvMessage;
    Context context;
//    ServerSocket server;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket_service);
        context = this;
        tvMessage = findViewById(R.id.et_message);
    }

    @Override
    protected void onDestroy() {
        if(server != null){
            server.stopService();
        }
        super.onDestroy();
    }

    public void onClickSocket(View v){
        switch (v.getId()){
            case R.id.btn_open_service:
                startSocketService();
                break;
            case R.id.btn_close_service:
                closeSockeetService();
                break;
            case R.id.btn_send_message:
                sendContent();
                break;
        }
    }

    int port = 8804;
    MySocketServer server;
    Thread t1;
    private void startSocketService(){
        server = new MySocketServer(port);
        Log.e(TAG, "Socket服务端已启动,端口号是:"+port);
        t1 = new Thread(new Runnable(){

            @Override
            public void run() {
                // TODO 自动生成的方法存根
                server.recDataFromClient();
            }

        });
        t1.start();
//        try{
//            ServerSocket ssock = new ServerSocket(8806);
//            System.out.println("Server Start and Listening: ");
//
//            while (true) {
//                Socket sock = ssock.accept();
//                System.out.println("Connected");
//                new Thread(new MultiThreadServer(sock)).start();
//            }
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
    }

    private void closeSockeetService(){
        if(server != null){
            server.stopService();
        }

    }

    private void sendContent(){
//        Intent intent = new Intent("com.example.myapplication.service.SocketService");
        Intent intent = new Intent(SocketServiceActivity.this,SocketService.class);
        intent.setAction("com.example.myapplication.service.SocketService");
        intent.setPackage("com.example.myapplication");
        //指定启动的是那个应用（lq.cn.twoapp）中的Action(b.aidl.DownLoadService)指向的服务组件
        startService(intent);


        //handReceiveMsg
//        ServerConfig.getInstance().setMessageProtocol(new IMessageProtocol() {
//            @Override
//            public int getHeaderLength() {
//                return 4;
//            }
//
//            @Override
//            public int getBodyLength(byte[] header, ByteOrder byteOrder) {
//                return header.length;
//            }
//
//            @Override
//            public byte[] pack(byte[] body) {
//                return new byte[0];
//            }
//        });
    }

}